package com.myl.admin.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.myl.admin.domain.dto.LoginBodyDTO;
import com.myl.admin.domain.vo.LoginInfoVO;
import com.myl.admin.service.ILoginService;
import com.myl.admin.service.ISysUserService;
import com.myl.admin.validate.ValidSequence;
import com.myl.core.domain.entity.SysUser;
import com.myl.core.utils.ResponseResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * LoginController
 * author: myl <ixiaomu@qq.com> 2024/1/19
 * tips:
 */
@Tag(name = "用户登录")
@RestController
public class LoginController {

    @Autowired
    private ILoginService iLoginService;

    @Autowired
    private ISysUserService iSysUserService;

    @Operation(summary = "用户登录")
    @PostMapping("/login")
    public ResponseResult<?> login(@RequestBody @Validated(ValidSequence.class) LoginBodyDTO loginBodyDTO) {
        Object data = iLoginService.doLogin(
                loginBodyDTO.getUsername(),
                loginBodyDTO.getPassword()
        );
        return ResponseResult.success("登陆成功", data);
    }

    @Operation(summary = "获取登录信息")
    @GetMapping("/login/info")
    public ResponseResult<?> info() {
        // 获取 Token
        Long loginId = StpUtil.getLoginIdAsLong();
        SysUser user = iSysUserService.getInfo(loginId, true);
        // 整理返回数据
        LoginInfoVO info = new LoginInfoVO(user);
        return ResponseResult.success("获取登录信息成功", info);
    }

    @Operation(summary = "安全退出")
    @PostMapping("/logout")
    public ResponseResult<?> logout() {
        return ResponseResult.success("安全退出成功");
    }
}
